package com.dwy2002.user;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dwy2002.common.Result;
import com.dwy2002.common.annotation.RequiresPermission;
import com.dwy2002.enums.Logical;
import com.dwy2002.pojo.dto.users.PermissionDTO;
import com.dwy2002.pojo.dto.users.PermissionPageDTO;
import com.dwy2002.pojo.po.users.Permission;
import com.dwy2002.pojo.vo.users.PermissionVO;
import com.dwy2002.user.service.IPermissionService;
import com.dwy2002.user.service.IRolePermissionService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 杜伟毅
 * @version 1.0
 * @since 2025/04/04
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/permission")
public class PermissionController {
    private final IPermissionService permissionService;
    private final IRolePermissionService rolePermissionService;

    /**
     * 添加权限
     *
     * @param dto 权限 DTO
     * @return 操作结果
     * @author 杜伟毅
     * @since 2025/4/4 0004 0:39
     */
    @RequiresPermission(value = {"A8"}, logical = Logical.OR)
    @PostMapping
    public Result<Void> addPermission(@RequestBody PermissionDTO dto) {
        return permissionService.addPermission(dto);
    }

    /**
     * 删除权限（根据ID删除）
     *
     * @param ids 权限id集合
     * @return 操作结果
     * @author 杜伟毅
     * @since 2025/4/4 0004 0:39
     */
    @RequiresPermission(value = {"A8"}, logical = Logical.OR)
    @DeleteMapping
    public Result<Void> deletePermission(@RequestBody List<String> ids) {
        return permissionService.deletePermission(ids);
    }

    /**
     * 更新权限（根据DTO中的ID更新）
     *
     * @param dto 权限 DTO
     * @return 操作结果
     * @author 杜伟毅
     * @since 2025/4/4 0004 0:39
     */
    @RequiresPermission(value = {"A8"}, logical = Logical.OR)
    @PutMapping
    public Result<Void> updatePermission(@RequestBody PermissionDTO dto) {
        return permissionService.updatePermission(dto);
    }

    /**
     * 查询单个权限（根据ID查询）
     *
     * @param id 权限id
     * @return 权限信息
     * @author 杜伟毅
     * @since 2025/4/4 0004 0:39
     */
    @RequiresPermission(value = {"A8"}, logical = Logical.OR)
    @GetMapping("/{id}")
    public Result<Permission> getPermissionById(@PathVariable String id) {
        return permissionService.getPermissionById(id);
    }

    /**
     * 分页查询权限列表
     *
     * @param dto 权限分页DTO
     * @return 分页结果
     * @author 杜伟毅
     * @since 2025/4/4 0004 0:39
     */
    @RequiresPermission(value = {"A8"}, logical = Logical.OR)
    @GetMapping("/page")
    public Result<Page<Permission>> getPermissionPage(@RequestBody PermissionPageDTO dto) {
        return permissionService.getPermissionPage(dto);
    }

    /**
     * 返回权限标识等级
     *
     * @return 权限等级列表
     * @author 杜伟毅
     * @since 2025/4/4 0004 0:39
     */
    @RequiresPermission(value = {"A8"}, logical = Logical.OR)
    @GetMapping("/level")
    public Result<List<String>> getPermLevel() {
        return permissionService.getPermLevel();
    }

    /**
     * 授予权限
     *
     * @param id 权限id
     * @author 杜伟毅
     * @since 2025/4/4 0004 10:46
     */
    @RequiresPermission(value = {"A8"}, logical = Logical.OR)
    @PostMapping("/grant/{id}")
    public Result<Void> grantPermission(@PathVariable String id) {
        return rolePermissionService.grantPermission(id);
    }

    /**
     * 撤销权限
     *
     * @param id 权限id
     * @author 杜伟毅
     * @since 2025/4/4 0004 11:09
     */
    @RequiresPermission(value = {"A8"}, logical = Logical.OR)
    @DeleteMapping("/revoke/{id}")
    public Result<Void> revokePermission(@PathVariable String id) {
        return rolePermissionService.revokePermission(id);
    }

    /**
     * 返回全部权限
     *
     * @author 杜伟毅
     * @since 2025/5/15 0015 0:35
     */
    @RequiresPermission(value = {"A8"}, logical = Logical.OR)
    @GetMapping
    public Result<List<PermissionVO>> getAllPermission() {
        return permissionService.getAllPermission();
    }
}
